package com.bbcare.followup.plat.controller.fd;

import com.bbcare.comm.ResultModel;
import com.bbcare.followup.plat.entity.DumpBreastRecord;
import com.bbcare.followup.plat.service.IDumpBreastRecordService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 复旦儿科泵乳记录Controller
 *
 * @author gxl
 * @date 2020-11-03
 */
@RestController
@RequestMapping("/webapi/service/dumpBreastRecord")
public class DumpBreastRecordController {
    @Autowired
    private IDumpBreastRecordService dumpBreastRecordService;

    /**
     * 查询复旦儿科泵乳记录列表
     */
    @PostMapping("/list")
    public ResultModel<Map<String, Object>> list(@RequestBody Map params) {
        Map map = new HashMap();
        Integer currentPage = Integer.parseInt(params.get("currentPage").toString());
        Integer pageSize = Integer.parseInt(params.get("pageSize").toString());
        params.put("start",pageSize*(currentPage-1));
        params.put("end",pageSize*currentPage);
        List<DumpBreastRecord> list = dumpBreastRecordService.selectDumpBreastRecordList(params);
        Integer total = dumpBreastRecordService.selectCountDumpBreastRecord(params);
        map.put("data", list);
        map.put("count",total);
        return new ResultModel(map);
    }

    @PostMapping("/patientList")
    public ResultModel<Map<String, Object>> patientList(@RequestBody Map params,HttpServletRequest request) {
        Map<String,Object> map = dumpBreastRecordService.selectDumpBreastPatientList(params,request);
        return new ResultModel(map);
    }

    /**
     * 获取复旦儿科泵乳记录详细信息
     */

    @PostMapping(value = "/getInfo")
    public ResultModel<Map<String, Object>> getInfo(@RequestBody Map param) {
        return new ResultModel(dumpBreastRecordService.selectDumpBreastRecordById(param.get("id").toString()));
    }

    /**
     * 新增复旦儿科泵乳记录
     */
    @PostMapping(value = "/add")
    public ResultModel<Map<String, Object>> add(@RequestBody DumpBreastRecord dumpBreastRecord) throws Exception {
        System.out.println("新增提交的参数"+ dumpBreastRecord.getIsUseDrug()+"  "+dumpBreastRecord.getUseDrugRemark());
        dumpBreastRecordService.insertDumpBreastRecord(dumpBreastRecord);

        final String userId = dumpBreastRecord.getPatientId();
        final String bedNo  = dumpBreastRecord.getBedNo();
        final String hospitalNo = dumpBreastRecord.getHospitalNo();
//        if (!bedNo.isEmpty() || !hospitalNo.isEmpty())  {
        if (StringUtils.isNotBlank(bedNo) || StringUtils.isNotBlank(hospitalNo))    {
            dumpBreastRecordService.updatePatientNo(userId, bedNo, hospitalNo);
        }

        return new ResultModel("添加成功");
    }

    /**
     * 修改复旦儿科泵乳记录
     */
    @PostMapping(value = "/update")
    public ResultModel<Map<String, Object>> edit(@RequestBody DumpBreastRecord dumpBreastRecord) throws Exception {
        dumpBreastRecordService.updateDumpBreastRecord(dumpBreastRecord);
        //TODO
        final String userId = dumpBreastRecord.getPatientId();
        final String bedNo  = dumpBreastRecord.getBedNo();
        final String hospitalNo = dumpBreastRecord.getHospitalNo();
        System.out.println("++++update bedno userid:"+userId+", bedno:"+bedNo);
        dumpBreastRecordService.updatePatientNo(userId, bedNo, hospitalNo);

        return new ResultModel("修改成功");
    }

    /**
     * 删除复旦儿科泵乳记录
     */
    @PostMapping("/delete")
    public ResultModel<Map<String, Object>> remove(@RequestBody String[] ids) {
        dumpBreastRecordService.deleteDumpBreastRecordByIds(ids);
        return new ResultModel("删除成功");
    }

    @PostMapping(value = "/getLastRecord")
    public ResultModel<Map<String, Object>> getLastRecord(@RequestBody Map param) {
        return new ResultModel(dumpBreastRecordService.selectLastDumpRecordById(param.get("patientId").toString()));
    }

    /**
     * 复旦儿科泵乳统计。
     * @param param
     * @return
     * 2021/11月新增需求。
     */
    @PostMapping(value = "/stat")
    public ResultModel stat(@RequestBody Map param)   {
        final String patientId = param.get("patient_id").toString();
        return new ResultModel(dumpBreastRecordService.statDumpRecordByPatientId(patientId));
    }
}
